﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TTTTBHYT
{
    public class  Module
    {
        public enum ClickButtonStatus
        {
            Them = 0,
            Sua = 1,
            Ghi = 2,
            Ngung = 3

        };
        public  enum Ketluan
        {
            Khongdungquydinh = 0,
            Dungquydinh = 1,
            Kcbtuchon = 2,
            Nhaptheongay = 3

        };
        public static String CreateRunninNo(Random rnd)
        {
            Char vt1, vt2, vt3;
            string st;
            vt1 = RandomChar(rnd);
            vt2 = RandomChar(rnd);
            vt3 = RandomChar(rnd);
            st = String.Format("{0:000}", DateTime.Now.Millisecond) + String.Format("{0:00}", rnd.Next(0, 99));
            return DateTime.Now.Year.ToString().Substring(2) +
                String.Format("{0:00}", DateTime.Now.Month) +
                String.Format("{0:00}", DateTime.Now.Day) +
                String.Format("{0:00}", DateTime.Now.Hour) +
                String.Format("{0:00}", DateTime.Now.Minute) +
                String.Format("{0:00}", DateTime.Now.Second) +
                st + vt1 + vt2 + vt3;

        }
        public static char RandomChar(Random rnd)
        {
            int a;
            Char vt;
            a = rnd.Next(55, 90);
            if (a < 65)
            {

                vt = (char)(a - 7);
            }
            else
            {
                vt = (char)a;
            }
            return vt;
        }
        public static void SendKey(Form frm, KeyEventArgs e)
        {

            try
            {

                if (((((e.KeyCode == Keys.Return) & !e.Alt) & !e.Control) & !e.Shift) && !((frm.ActiveControl.Parent is C1.Win.C1FlexGrid.C1FlexGrid) | (frm.ActiveControl is C1.Win.C1FlexGrid.C1FlexGrid)))
                {

                    if (frm.GetNextControl(frm.ActiveControl, true) != null)
                    {
                        SendKeys.Send("{TAB}");
                    }
                    else
                    {
                        while (frm.GetNextControl(frm.ActiveControl, true) == null)
                        {

                            if (!frm.GetNextControl(frm.ActiveControl, true).Enabled)
                            {
                                frm.ActiveControl = frm.GetNextControl(frm.ActiveControl, true);
                            }
                            else
                            {
                                if (!frm.GetNextControl(frm.ActiveControl, true).Visible)
                                {
                                    if (frm.GetNextControl(frm.ActiveControl, true) is TabPage)
                                    {
                                        frm.ActiveControl = frm.GetNextControl(frm.ActiveControl, true);
                                        return;
                                    }
                                    frm.ActiveControl = frm.GetNextControl(frm.ActiveControl, true);
                                    continue;
                                }
                                if (frm.GetNextControl(frm.ActiveControl, true) is GroupBox)
                                {
                                    frm.GetNextControl(frm.ActiveControl, true).Focus();
                                    frm.GetNextControl(frm.ActiveControl, true).Focus();
                                    continue;
                                }
                                if (frm.GetNextControl(frm.ActiveControl, true) is TabControl)
                                {
                                    frm.ActiveControl = frm.GetNextControl(frm.ActiveControl, true);
                                    continue;
                                }
                                if (frm.GetNextControl(frm.ActiveControl, true) is TabPage)
                                {
                                    frm.ActiveControl = frm.GetNextControl(frm.ActiveControl, true);
                                    continue;
                                }
                                if (frm.GetNextControl(frm.ActiveControl, true) is RadioButton)
                                {
                                    if (frm.ActiveControl is RadioButton)
                                    {
                                        SendKeys.Send("{TAB}");
                                    }
                                    else
                                    {
                                        frm.ActiveControl = frm.GetNextControl(frm.ActiveControl, true);
                                    }
                                    return;
                                }
                                if (frm.GetNextControl(frm.ActiveControl, true) is Label)
                                {
                                    frm.GetNextControl(frm.ActiveControl, true).Focus();
                                    frm.GetNextControl(frm.ActiveControl, true).Focus();
                                }
                                else
                                {
                                    frm.GetNextControl(frm.ActiveControl, true).Focus();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exception1)
            {

                Exception exception = exception1;
                throw exception;

            }
        }
        public static string Doc_tien_sang_tieng_viet(double N, string DviChan = "", string DviLe = "")
        {

            string Ketqua = null;
            string sotien = null;
            string nhom = null;
            string chu = null;
            string dich = null;
            string s1 = null;
            string S2 = null;
            string s3 = null;
            byte i = 0;
            byte j = 0;
            string[] Hang = new string[4]  ;
            string[] doc = new string[7];
            string[] dem = new string[10];
            double s = 0;

            if (N == 0)
            {
                Ketqua = " kh«ng " + DviChan;
            }
            else
            {
                if (N < 0)
                {
                    Ketqua = "trõ" + Microsoft.VisualBasic.Strings.Space(1);
                }
                else
                {
                    Ketqua = Microsoft.VisualBasic.Strings.Space(0);
                }
                //If InStr(CType(N, String), ",") > 0 Then
                //    DviChan = "phẩy"
                //Else
                //    DviChan = DviLe
                //    DviLe = ""
                //End If
                sotien = Microsoft.VisualBasic.Strings.Format(Math.Abs(N), "##############0.00");
                sotien = Microsoft.VisualBasic.Strings.Right(Microsoft.VisualBasic.Strings.Space(15) + sotien, 18);
                Hang[0] = "";
                Hang[1] = "tr¨m";
                Hang[2] = "m­¬i";
                Hang[3] = "ï";
                //Hang = Array("", "traêm", "möôi", "ù");
                doc[0] = "";
                doc[1] = "ngµn tû";
                doc[2] = "tû";
                doc[3] = "triÖu";
                doc[4] = "ngµn";
                doc[5] = DviChan;
                doc[6] = DviLe;
                dem[0] = "";
                dem[1] = "mét";
                dem[2] = "hai";
                dem[3] = "ba";
                dem[4] = "bèn";
                dem[5] = "n¨m";
                dem[6] = "s¸u";
                dem[7] = "b¶y";
                dem[8] = "t¸m";
                dem[9] = "chÝn";
                for (i = 1; i <= 6; i++)
                {
                    nhom = Microsoft.VisualBasic.Strings.Mid(sotien, i * 3 - 2, 3);
                    if (nhom != Microsoft.VisualBasic.Strings.Space(3))
                    {
                        switch (nhom)
                        {
                            case "000":
                                if (i == 5)
                                {
                                    chu = DviChan + Microsoft.VisualBasic.Strings.Space(1);
                                }
                                else
                                {
                                    chu = Microsoft.VisualBasic.Strings.Space(0);
                                }
                                break;
                            case ".00":
                            case ",00":
                                chu = "";
                                break;
                            default:
                                s1 = Microsoft.VisualBasic.Strings.Left(nhom, 1);
                                S2 = Microsoft.VisualBasic.Strings.Mid(nhom, 2, 1);
                                s3 = Microsoft.VisualBasic.Strings.Right(nhom, 1);
                                chu = Microsoft.VisualBasic.Strings.Space(0);
                                Hang[3] = doc[i];
                                for (j = 1; j <= 3; j++)
                                {
                                    dich = Microsoft.VisualBasic.Strings.Space(0);
                                    s = Microsoft.VisualBasic.Conversion.Val(Microsoft.VisualBasic.Strings.Mid(nhom, j, 1));
                                    if (s > 0)
                                    {
                                        dich = dem[Convert.ToInt16(s)] + Microsoft.VisualBasic.Strings.Space(1) + Hang[j] + Microsoft.VisualBasic.Strings.Space(1);
                                    }
                                    switch (j)
                                    {
                                        case 2:
                                            if (Convert.ToInt16(s) == 1)
                                            {
                                                dich = "m­êi" + Microsoft.VisualBasic.Strings.Space(1);
                                            }
                                            //if (( Convert.ToInt16(s1) >= 1 & Convert.ToInt16(s1) <= 9) || (Convert.ToInt16(s1) == 0 && i == 4) && s == 0 && Convert.ToInt16(s3) != 0) {
                                            //    dich = "linh" + Microsoft.VisualBasic.Strings.Space(1);
                                            //}
                                            break;
                                        case 3:
                                            if (Convert.ToInt16(s) == 0 && nhom != Microsoft.VisualBasic.Strings.Space(2) + "0")
                                            {
                                                dich = Hang[j] + Microsoft.VisualBasic.Strings.Space(1);
                                            }
                                            if (s == 5 & S2 != Microsoft.VisualBasic.Strings.Space(1) & S2 != "0")
                                            {
                                                dich = "l" + Microsoft.VisualBasic.Strings.Mid(dich, 2);
                                            }
                                            break;

                                    }
                                    chu = chu + dich;
                                }

                                break;
                        }
                        //vitri = Microsoft.VisualBasic.Strings.InStr(1, chu, "mươi một", 1);
                        //if (vitri > 0)
                        //    Microsoft.VisualBasic.Strings.Mid(chu, vitri, 9) = "mươi mốt";
                        return Ketqua + chu;
                    }
                }
            }

            return Microsoft.VisualBasic.Strings.UCase(Microsoft.VisualBasic.Strings.Left(Ketqua, 1)) + Microsoft.VisualBasic.Strings.Mid(Ketqua, 2);
        }
        public static string ChangeToCamel(string str)
        {
            string[] strArr = str.Split(new char[] { ' ' });
            StringBuilder strTemp = new StringBuilder();

            foreach (string subStr in strArr)
            {
                if (subStr != "")
                {
                    if (subStr.Substring(0, 1) != "ñ")
                    {
                        strTemp.Append(subStr.Substring(0, 1).ToUpper()).Append(subStr.Substring(1, subStr.Length - 1)).Append(" ");
                    }
                    else
                    {
                        strTemp.Append(subStr.Substring(0, 1)).Append(subStr.Substring(1, subStr.Length - 1)).Append(" ");
                    }
                }
            }
            return strTemp.ToString();
        }
       

        private static string Chu(string gNumber)
        {
            string result = "";
            switch (gNumber)
            {
                case "0":
                    result = "không";
                    break;
                case "1":
                    result = "một";
                    break;
                case "2":
                    result = "hai";
                    break;
                case "3":
                    result = "ba";
                    break;
                case "4":
                    result = "bốn";
                    break;
                case "5":
                    result = "năm";
                    break;
                case "6":
                    result = "sáu";
                    break;
                case "7":
                    result = "bảy";
                    break;
                case "8":
                    result = "tám";
                    break;
                case "9":
                    result = "chín";
                    break;
            }
            return result;
        }
        private static string Donvi(string so)
        {
            string Kdonvi = "";

            if (so.Equals("1"))
                Kdonvi = "";
            if (so.Equals("2"))
                Kdonvi = "nghìn";
            if (so.Equals("3"))
                Kdonvi = "triệu";
            if (so.Equals("4"))
                Kdonvi = "tỷ";
            if (so.Equals("5"))
                Kdonvi = "nghìn tỷ";
            if (so.Equals("6"))
                Kdonvi = "triệu tỷ";
            if (so.Equals("7"))
                Kdonvi = "tỷ tỷ";

            return Kdonvi;
        }
        private static string Tach(string tach3)
        {
            string Ktach = "";
            if (tach3.Equals("000"))
                return "";
            if (tach3.Length == 3)
            {
                string tr = tach3.Trim().Substring(0, 1).ToString().Trim();
                string ch = tach3.Trim().Substring(1, 1).ToString().Trim();
                string dv = tach3.Trim().Substring(2, 1).ToString().Trim();
                if (tr.Equals("0") && ch.Equals("0"))
                    Ktach = " không trăm lẻ " + Chu(dv.ToString().Trim()) + " ";
                if (!tr.Equals("0") && ch.Equals("0") && dv.Equals("0"))
                    Ktach = Chu(tr.ToString().Trim()).Trim() + " trăm ";
                if (!tr.Equals("0") && ch.Equals("0") && !dv.Equals("0"))
                    Ktach = Chu(tr.ToString().Trim()).Trim() + " trăm lẻ " + Chu(dv.Trim()).Trim() + " ";
                if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                    Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mươi " + Chu(dv.Trim()).Trim() + " ";
                if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && dv.Equals("0"))
                    Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mươi ";
                if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && dv.Equals("5"))
                    Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mươi lăm ";
                if (tr.Equals("0") && ch.Equals("1") && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                    Ktach = " không trăm mười " + Chu(dv.Trim()).Trim() + " ";
                if (tr.Equals("0") && ch.Equals("1") && dv.Equals("0"))
                    Ktach = " không trăm mười ";
                if (tr.Equals("0") && ch.Equals("1") && dv.Equals("5"))
                    Ktach = " không trăm mười lăm ";
                if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mươi " + Chu(dv.Trim()).Trim() + " ";
                if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && dv.Equals("0"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mươi ";
                if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && dv.Equals("5"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mươi lăm ";
                if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm mười " + Chu(dv.Trim()).Trim() + " ";

                if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && dv.Equals("0"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm mười ";
                if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && dv.Equals("5"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm mười lăm ";

            }


            return Ktach;

        }
        public static string So_chu(double gNum)
        {
            if (gNum == 0)
                return "Không đồng";

            string lso_chu = "";
            string tach_mod = "";
            string tach_conlai = "";
            double Num = Math.Round(gNum, 0);
            string gN = Convert.ToString(Num);
            int m = Convert.ToInt32(gN.Length / 3);
            int mod = gN.Length - m * 3;
            string dau = "[+]";

            // Dau [+ , - ]
            if (gNum < 0)
                dau = "[-]";
            dau = "";

            // Tach hang lon nhat
            if (mod.Equals(1))
                tach_mod = "00" + Convert.ToString(Num.ToString().Trim().Substring(0, 1)).Trim();
            if (mod.Equals(2))
                tach_mod = "0" + Convert.ToString(Num.ToString().Trim().Substring(0, 2)).Trim();
            if (mod.Equals(0))
                tach_mod = "000";
            // Tach hang con lai sau mod :
            if (Num.ToString().Length > 2)
                tach_conlai = Convert.ToString(Num.ToString().Trim().Substring(mod, Num.ToString().Length - mod)).Trim();

            ///don vi hang mod
            int im = m + 1;
            if (mod > 0)
                lso_chu = Tach(tach_mod).ToString().Trim() + " " + Donvi(im.ToString().Trim());
            /// Tach 3 trong tach_conlai

            int i = m;
            int _m = m;
            int j = 1;
            string tach3 = "";
            string tach3_ = "";

            while (i > 0)
            {
                tach3 = tach_conlai.Trim().Substring(0, 3).Trim();
                tach3_ = tach3;
                lso_chu = lso_chu.Trim() + " " + Tach(tach3.Trim()).Trim();
                m = _m + 1 - j;
                if (!tach3_.Equals("000"))
                    lso_chu = lso_chu.Trim() + " " + Donvi(m.ToString().Trim()).Trim();
                tach_conlai = tach_conlai.Trim().Substring(3, tach_conlai.Trim().Length - 3);

                i = i - 1;
                j = j + 1;
            }
            if (lso_chu.Trim().Substring(0, 1).Equals("k"))
                lso_chu = lso_chu.Trim().Substring(10, lso_chu.Trim().Length - 10).Trim();
            if (lso_chu.Trim().Substring(0, 1).Equals("l"))
                lso_chu = lso_chu.Trim().Substring(2, lso_chu.Trim().Length - 2).Trim();
            if (lso_chu.Trim().Length > 0)
                lso_chu = dau.Trim() + " " + lso_chu.Trim().Substring(0, 1).Trim().ToUpper() + lso_chu.Trim().Substring(1, lso_chu.Trim().Length - 1).Trim() + " đồng.";

            return lso_chu.ToString().Trim();

        }

        public static int Tron100(int num)
        {
            int chuc;
            int tron100;
            chuc = (num / 10) % 10;
            tron100 = (num - (num % 100));
            if (chuc >= 5) { tron100 = tron100 + 100; }
            return tron100;
        }
    }
}
